﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PreContractWeb.Task
{
    public partial class KeyProjectSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.FillInReportDropdown_Adjust();

                BEGDC.PreContract.BLL.IKeyProjectDefaultAmount amountservice = (BEGDC.PreContract.BLL.IKeyProjectDefaultAmount)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_KeyProjectDefaultAmount_Service];
                BEGDC.PreContract.DAL.KeyProjectDefaultAmount amount = amountservice.GetKeyProjectDefaultAmount(1);
                this.txtOrionME.Text = amount.DefaultAmount.Value.ToString();
            }
        }

        protected string SearchSQL
        {
            get
            {
                if (Session["SearchSQL"] == null) return string.Empty;
                return Session["SearchSQL"] as string;
            }
            set
            {
                Session["SearchSQL"] = value;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strSQL = string.Empty;
            strSQL += " StatusID in (10,11) AND ";

            try
            {
                decimal dec = System.Convert.ToDecimal(this.txtOrionME.Text.Trim());
            }
            catch 
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + "预计转正金额必须是数字.\\nOrion ME should be digtals." + "')</script>");
                return;
            }

            //Joan 2008-11-05 
            if (rdByNo.Checked)
            {
                //search by pre-contract no, sum all BD ME > the ammount
                if (this.txtOrionME.Text.Trim() != string.Empty)
                    strSQL += string.Format(" PreContractNo IN (SELECT PreContractNo FROM vw_PreContractInfo WHERE  StatusID in (10,11) GROUP BY PreContractNo HAVING SUM(OrionME) >= {0}) AND ", this.txtOrionME.Text.Trim());
            }
            else
            {
                //search by pre-contract no & BD

                if (this.txtOrionME.Text.Trim() != string.Empty)
                    strSQL += string.Format(" OrionME >= {0} AND ", this.txtOrionME.Text.Trim());
            }

            //yu liang 2010-5-20
            if (int.Parse(ddlAdjust.SelectedItem.Value) > 0)
            {
                strSQL += string.Format(" AdjustID = {0} AND ", int.Parse(ddlAdjust.SelectedItem.Value));
            }

            //end

            if (strSQL.EndsWith("AND "))
                strSQL = strSQL.Substring(0, strSQL.Length - 4);
            

            this.SearchSQL = strSQL;

            Page.Response.Redirect("AllPre-Contracts.aspx?Search=1&guid=" + System.Guid.NewGuid().ToString("N") + "'");

        }

        private void FillInReportDropdown_Adjust()
        {
            BaseFunction.BindContractAdjustDropDownList(ddlAdjust, string.Empty, true);
        }
    }
}
